/**
 * 
 */
package org.cuatrovientos.erp.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.RoleBOInterface;
import org.cuatrovientos.erp.model.Role;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;



/**
 * Controller class form Role management
 * @author pablo_guerrero
 */
@Controller
public class RoleController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(RoleController.class);

	@Autowired
    private RoleBOInterface roleBO;
    
        /**
         * default constructor
         */
        public RoleController () {
               logger.info("Role BEAN instantiated");
                
        }
        
        
        
        /**
         * handles default /role or /role/list request
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/role","/role/list"})
         */
        @RequestMapping({"/role","/role/list"})
        public ModelAndView userDefault() {
        	logger.info("default");
 
                Map<String, Object> model = new HashMap<String, Object>();

                List<Role> roleList = roleBO.selectAll();
                model.put("roles", roleList );
                return new ModelAndView("RoleList", model);     
        }

        /**
         * handles /role/search request, redirected to default page
         * @param model
         * @return the name of the view to show
         * @RequestMapping({"/role/search"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/role/search"})
        public ModelAndView search(@RequestParam(value="description", defaultValue="", required=true) String description) {
        	logger.info("search");
 
            Map<String, Object> model = new HashMap<String, Object>();

            List<Role> roleList = roleBO.search(description);
            model.put("roles", roleList );
            
            return new ModelAndView("RoleList", model);     
        }

        /**
         * handles delete role, and redirect to user default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/role","/role/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/role/delete"})
        public String deleteRole(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("delete " + id);

        	roleBO.deleteById(id);
            return "redirect:/role";

        }
        
        /**
         * handles new role request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it: in this case "role"
         * @return the name of the view to show
         * @RequestMapping({"/role/new"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/role/new"})
        public ModelAndView newRole() {
        		logger.info("new role ");
                Role role = new Role();
               
                return new ModelAndView("RoleNew", "role", role);
        }
        
        /**
         * handles new role request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/role/new"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/role/save"})
        public String newRole(@Valid Role role, BindingResult bindingResult) {
        		logger.info("new role " + role.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("role", role);
        			return "RoleNew";
        		}
        		
        		// If data is ok, insert and go on
                roleBO.insert(role);
                return "redirect:/role";
        }
        
        /**
         * handles delete role, and redirect to role default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/role","/role/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/role/detail"})
        public ModelAndView roleDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("Detail " + id);
            Map<String,Object> model = new HashMap<String,Object>();
            Role role = roleBO.selectById(id);
            model.put("role", role);
            
            return new ModelAndView("RoleDetail",model);

        }
        
        /**
         * handles role update, first part: register loaded
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/role/update"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/role/update"})
        public ModelAndView roleUpdate(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("update " + id);
                Map<String,Object> model = new HashMap<String,Object>();
                Role role = roleBO.selectById(id);
                model.put("role", role);
               return new ModelAndView("RoleUpdate",model);

        }
        
        /**
         * handles save update role request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/role/saveUpdate"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/role/saveupdate"})
        public String saveUpdate(@Valid Role role, BindingResult bindingResult) {
        		logger.info("update role " + role.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("role", role);
        			return "RoleUpdate";
        		}
        		
        		roleBO.update(role);
                return "redirect:/role";
        }
}
